package vip.indo.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

/**
 * 使用ServletRequestListener获取访问信息
 * @Author: tianlianyin
 * @Date: 2024/11/28 16:51
 * @Descripition:
 */
@Component
@Slf4j
public class MyServletRequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        HttpServletRequest request= (HttpServletRequest) servletRequestEvent.getServletRequest();
        log.info("session id为：{}",request.getRequestedSessionId());
        log.info("请求路径为：{}",request.getRequestURI());
        request.setAttribute("name","测试");
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        log.info("request结束");
        HttpServletRequest request= (HttpServletRequest) servletRequestEvent.getServletRequest();
        log.info("request域中保存的name值为：{}",request.getAttribute("name"));
    }
}
